{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Python基础"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "注意事项：\n",
    "* 请区分全角半角的符号\n",
    "* 请使用Python 3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 这是一行注释\n",
    "# 自由空间\n",
    "2 + 3 * 4\n",
    "\n",
    "# 有趣的Python\n",
    "# 迷宫\n",
    "print(''.join(__import__('random').choice('\\u2571\\u2572') for i in range(50*24)))\n",
    "\n",
    "# 乘法表\n",
    "print('\\n'.join([' '.join(['%s*%s=%-2s' % (y,x,x*y) for y in range(1,x+1)]) for x in range(1,10)]))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 数据类型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#整型\n",
    "print(2006, 0xffff, 0b101101, 6546546546546546546546565654654635)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#浮点型\n",
    "print(1.234, -1.23e11)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#字符串\n",
    "str1 = \"Hello, \"\n",
    "str2 = 'World!'\n",
    "str0 = str1 + str2\n",
    "str0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(len(str0))\n",
    "print(str0[0], str0[-1], str0[3:5])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#布尔值\n",
    "print(3 > 2, True or False, True and False, not True, 3 > 6 and 1 > 5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 使用 type() 来判断类型\n",
    "type(str0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#List 是有序且可变的列表\n",
    "\n",
    "list0 = [\"apple\", \"pear\"]\n",
    "list0.append(\"orange\")\n",
    "print(list0)\n",
    "\n",
    "list0.insert(0, \"mango\")\n",
    "print(list0)\n",
    "\n",
    "list0.remove('mango')\n",
    "print(list0)\n",
    "\n",
    "list0.pop()\n",
    "print(list0)\n",
    "\n",
    "list0.append(555)\n",
    "print(list0)\n",
    "\n",
    "list0[0] = 'lll'\n",
    "print(list0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Tuple 是有序但不可变的列表\n",
    "tuple0 = (1, 2)\n",
    "print(tuple0[1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Set 用大括号构造\n",
    "s1 = {1, 2, 3, 4}\n",
    "print(s1)\n",
    "\n",
    "s2 = {1, 1}\n",
    "print(s2)\n",
    "\n",
    "# Set 添加元素\n",
    "s2.add(2)\n",
    "print(s2)\n",
    "\n",
    "# Set 删除元素\n",
    "s2.remove(1)\n",
    "print(s2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Dict 字典，使用key-value存储，查找速度快\n",
    "# 传统方法\n",
    "names = [\"Robert\", \"Michael\", \"Bob\"]\n",
    "ages = [18, 20, 27]\n",
    "\n",
    "# 使用字典,  大括号\n",
    "d = {\"Robert\" : 18, \"Michael\" : 20, \"Bob\" : 27}\n",
    "d[\"Bob\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 添加值\n",
    "print(d)\n",
    "d['Lucy'] = 14\n",
    "print(d)\n",
    "\n",
    "# 修改值\n",
    "d['Lucy'] = 15\n",
    "print(d)\n",
    "\n",
    "# 删除索引\n",
    "d.pop('Bob')\n",
    "print(d)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 小练习\n",
    "# 定义一个含有3个元素的tuple\n",
    "\n",
    "# 显示上面tuple的最后一项\n",
    "\n",
    "# 将上面的tuple转换成set"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 条件判断与循环"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 条件判断\n",
    "size = 30\n",
    "if size >= 20:\n",
    "    print(size, \" is big\")\n",
    "elif size >= 10:\n",
    "    print(size, \" is middle\")\n",
    "else:\n",
    "    print(size, \" is small\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 循环\n",
    "fruits = [\"apple\", \"pear\", \"banana\"]\n",
    "for name in fruits:\n",
    "    print(name)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "list(range(-1, -10, -1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 循环\n",
    "for x in range(5):\n",
    "    print(x)\n",
    "\n",
    "for y in range(100, 105, 2):\n",
    "    print(y)\n",
    "    \n",
    "for col in range(3):\n",
    "    for row in range(3):\n",
    "        print(col, row)\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 计算 1+2+3+。。。+100的值\n",
    "sum = 0\n",
    "for x in range(101):\n",
    "    sum += x\n",
    "print(sum)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 小练习  求 10！\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
